package com.ssy.wireless.api.service;

import com.ssy.wireless.entity.ApDevice;
import com.ssy.wireless.entity.MessageAuth;
import com.ssy.wireless.mapper.ApDeviceMapper;
import com.ssy.wireless.mapper.MessageAuthMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * Created by cofey on 16/11/21.
 */
@Service
@Transactional(readOnly = true)
public class ApiMessageAuthService {
    @Autowired
    private MessageAuthMapper messageAuthMapper;

    @Autowired
    private ApDeviceMapper apDeviceMapper;

    /**
     * 用户请求
     *
     * @param messageAuth
     */
    @Transactional
    public void insertRequestInfo(MessageAuth messageAuth) {
        ApDevice apDevice = apDeviceMapper.selectByApIndex(messageAuth.getApIndex());
        Assert.notNull(apDevice, "当前AP索引[" + messageAuth.getApIndex() + "]不存在");

        Date date = new Date();
        messageAuth.setApDeviceId(apDevice.getId());
        messageAuth.setHotspotId(apDevice.getHotspotId());
        messageAuth.setEntId(apDevice.getEntId());
        messageAuth.setRequetTime(null != messageAuth.getRequetTime() ? messageAuth.getRequetTime() : date);
        messageAuth.setHotspotId(apDevice.getHotspotId());
        messageAuth.setApDeviceId(apDevice.getId());
        messageAuthMapper.insert(messageAuth);
    }

    /**
     * 用户发送短信
     *
     * @param messageAuth
     */
    @Transactional
    public void updateSendInfo(MessageAuth messageAuth) {
        ApDevice apDevice = apDeviceMapper.selectByApIndex(messageAuth.getApIndex());
        Assert.notNull(apDevice, "当前AP索引[" + messageAuth.getApIndex() + "]不存在");

        Long id = messageAuthMapper.selectAuthIdByApIndexAndMobile(messageAuth.getMobile(), apDevice.getId());
        Assert.notNull(id, "未找到当前设备下用户请求记录信息");
        Date date = new Date();
        messageAuth.setId(id);
        messageAuth.setSendTime(null != messageAuth.getSendTime() ? messageAuth.getSendTime() : date);
        messageAuthMapper.updateByPrimaryKey(messageAuth);
    }

    /**
     * 用户登录
     *
     * @param messageAuth
     */
    @Transactional
    public void updateLoginInfo(MessageAuth messageAuth) {
        ApDevice apDevice = apDeviceMapper.selectByApIndex(messageAuth.getApIndex());
        Assert.notNull(apDevice, "当前AP索引[" + messageAuth.getApIndex() + "]不存在");

        Long id = messageAuthMapper.selectAuthIdByApIndexAndMobile(messageAuth.getMobile(), apDevice.getId());
        Assert.notNull(id, "未找到当前设备下用户短信发送记录信息");
        Date date = new Date();
        messageAuth.setId(id);
        messageAuth.setLoginTime(null != messageAuth.getLoginTime() ? messageAuth.getLoginTime() : date);
        messageAuth.setUpTime(null != messageAuth.getUpTime() ? messageAuth.getUpTime() : date);
        messageAuthMapper.updateByPrimaryKey(messageAuth);
    }

    /**
     * 用户下线
     *
     * @param messageAuth
     */
    @Transactional
    public void updateDownInfo(MessageAuth messageAuth) {
        ApDevice apDevice = apDeviceMapper.selectByApIndex(messageAuth.getApIndex());
        Assert.notNull(apDevice, "当前AP索引[" + messageAuth.getApIndex() + "]不存在");

        Long id = messageAuthMapper.selectAuthIdByApIndexAndMobile(messageAuth.getMobile(), apDevice.getId());
        Assert.notNull(id, "未找到当前设备下用户登录记录信息");
        Date date = new Date();
        messageAuth.setId(id);
        messageAuth.setDownTime(null != messageAuth.getDownTime() ? messageAuth.getDownTime() : date);
        messageAuthMapper.updateByPrimaryKey(messageAuth);
    }

}
